package IOxuliehua;

import IOxuliehua.Dog;

import java.io.*;
import java.util.Arrays;
//对象序列化
//把对象写入文件：实际写入的是类名.属性名.属性类型.属性的值等

//反序列化的过程、
//从文件中把对象的内容读取出来，还原成对象
public class ObjectStreamDemo {
    private static void readerObject(){
        File file = new File("C:\\Users\\Administrator\\Desktop\\File\\dog.obj");
        try {
            InputStream in = new FileInputStream(file);
            ObjectInputStream is = new ObjectInputStream(in);
          Dog[] dog = (Dog[]) is.readObject();
            System.out.println(Arrays.toString(dog));
            is.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }


    }
    private static void writeObject(){
        Dog dog = new Dog("旺旺",2,"母狗");
        Dog dog2 = new Dog("2旺",1,"2狗");
            Dog[] dogs = {dog,dog2};
        File file = new File("C:\\Users\\Administrator\\Desktop\\File\\dog.obj");
        try {
            OutputStream out = new FileOutputStream(file);
            ObjectOutputStream os = new ObjectOutputStream(out);
            os.writeObject(dogs);
            os.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    public static void main(String[] args) {
        writeObject();
             readerObject();
    }

}
